package leetcode.median;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class $18_FourSum {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> fourSum(int[] nums, int target) {
        Arrays.sort(nums);
        bp(nums, target, 0);
        return res;
    }

    public void bp(int nums[], int target, int index) {
        if (path.size() == 4 && target == 0) {
            res.add(new ArrayList<>(path));
            return;
        }

        for (int i = index;i < nums.length;i++) {
            if (i > index && nums[i] == nums[i - 1]) {
                continue;
            }
            path.add(nums[i]);
            bp(nums, target - nums[i], i + 1);
            path.remove(path.size() - 1);
            bp(nums, target, i + 1);
        }
    }
}
